package cn.innosmart.aq.util;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;

/**
 * Created by cuber on 2014/12/8.
 */
public class FragmentManagerTool {

    /* init Fragment */
    public static void init(Bundle savedInstanceState, FragmentManager fragmentManager, int layoutID, Fragment fragment) {
        FragmentTransaction ft = fragmentManager.beginTransaction();
        ft.setCustomAnimations(android.R.anim.fade_in, android.R.anim.fade_out, android.R.anim.fade_in, android.R.anim.fade_out
        );
        if (savedInstanceState == null) {
            ft.add(layoutID, fragment).commitAllowingStateLoss();
        }
    }

    /* replace Fragment */
    public static void replaceFragment(FragmentManager fragmentManager, Fragment f, int layout) {
        FragmentTransaction ft = fragmentManager.beginTransaction();
        ft.setCustomAnimations(android.R.anim.fade_in, android.R.anim.fade_out, android.R.anim.fade_in, android.R.anim.fade_out);
        ft.replace(layout, f);
        ft.commit();
    }

    /* add Fragment */
    public static void addToBackStackFragment(FragmentManager fragmentManager, Fragment f, int layout) {
        FragmentTransaction ft = fragmentManager.beginTransaction();
        ft.setCustomAnimations(android.R.anim.fade_in, android.R.anim.fade_out, android.R.anim.fade_in, android.R.anim.fade_out);
        ft.replace(layout, f);
        ft.addToBackStack(null);
        ft.commit();
    }

    /* Pop Back */
    public static void popBackStackFragment(FragmentManager fragmentManager) {
        fragmentManager.popBackStack();
    }

    /* clear (�^����) */
    public static void CleanFragment(FragmentManager fragmentManager) {
        // // Clear all back stack.
        int backStackCount = fragmentManager.getBackStackEntryCount();
        for (int i = 0; i < backStackCount; i++) {
            // Get the back stack fragment id.
            int backStackId = fragmentManager.getBackStackEntryAt(i).getId();
            fragmentManager.popBackStack(backStackId, FragmentManager.POP_BACK_STACK_INCLUSIVE);
        }
    }

    /* remove */
    public void removeFragment(FragmentManager fragmentManager, Fragment f) {
        FragmentTransaction ft = fragmentManager.beginTransaction();
        ft.remove(f);
        ft.commit();
    }
}